package com.wenx.bloomfilter;

import com.google.common.collect.Lists;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.UUID;

/**
 * 布隆过滤器
 */
public class BloomFilterMain {

    //30%误判率
    static BloomFilter bloomFilter = BloomFilter.create(Funnels.stringFunnel(Charset.defaultCharset()), 1000,0.3D);


    public static void main(String[] args) {

        ArrayList<String> objects = Lists.newArrayList();

        for (int i = 0; i < 1000; i++) {

            String s = UUID.randomUUID().toString();

            bloomFilter.put(s);
            if(i%2 == 0)
                objects.add(s);

        }

        for (int i = 0; i < 500; i++) {
            String s = UUID.randomUUID().toString();
            objects.add(s);
        }


        int okNum = 0;
        int errorNum = 0;

        for (String str : objects) {
            if (bloomFilter.mightContain(str)) {
                okNum++;
            } else {
                errorNum++;
            }
        }

        System.out.println("okNum = " + okNum);
        System.out.println("errorNum = " + errorNum);


    }

}
